// pages/registerPersonal/registerPersonal.js
// 导入dialog
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const {
    sendSms,
    checkSms,
    registerNow,
} = require('../../services/registerPersonalReq');
// 统一资源信息管理
// import {
//   areaList,
//   OccupationList
// } from "../../utils/pablicInfo";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userinfo: {},
        isSend: false,
        // 短信倒计时
        timerCount: 60,
        // 表单信息
        volunteerAccount: '',
        volunteerPass: '',
        volunteerName: '',
        volunteerSex: '',
        volunteerMobile: '',
        volunteerNo: '',
        smsCode: '',
        volunteerAvatar: '',
        //  修改状态
        powerShow: true
    },
    onLoad: function (options) {

    },
    // 立即注册
    handleRegisterNow() {
        const volunteerAccount = this.data.volunteerAccount
        const volunteerPass = this.data.volunteerPass
        const volunteerName = this.data.volunteerName
        const volunteerSex = this.data.volunteerSex
        const volunteerMobile = this.data.volunteerMobile
        const smsCode = this.data.smsCode
        const registerObj = {
            volunteerAccount,
            volunteerPass,
            volunteerName,
            volunteerSex,
            volunteerMobile,
        }
        // 先校验短信
        wx.showLoading({
            title: "注册中",
            mask: true
        })
        checkSms(volunteerMobile, smsCode, res => {
            wx.login({
                success: res => {
                    registerObj.code = res.code
                    registerObj.volunteerAvatar = this.data.volunteerAvatar
                    // wx.getImageInfo({
                    //     src: userInfo.avatarUrl,
                    //     success: function (sres) {
                    //         //上传图片
                    //         wx.uploadFile({
                    //             url: app.globalData.serverUrl + '/uploadHead?openId=' + app.globalData.openId,
                    //             filePath: sres.path,
                    //             name: "file",
                    //             success: function (res) {
                    //                 console.log(res)
                    //             },
                    //             error: function (rev) {
                    //                 console.log(rev);
                    //             }
                    //         });
                    //     },
                    //     fail: function (srev) {
                    //         console.log(srev);
                    //     }
                    // });
                    // 执行注册
                    registerNow(registerObj, res => {
                        wx.hideLoading()
                        if (res.data.code === 0) {
                            wx.showToast({
                                title: '注册成功',
                                icon: 'success'
                            })
                            // 提示补充信息
                            Dialog.confirm({
                                title: '提示',
                                message: '请立即登陆并修改信息'
                            }).then(() => {
                                return wx.navigateTo({
                                    url: '../login/login',
                                })
                            }).catch(() => {
                                return null;
                            })
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            })
                        }
                    })
                },
                fail: res => {
                    wx.showToast({
                        title: '微信登录失败',
                        icon: 'none'
                    })
                }
            })

        })
    },

    // 发送短信验证码
    handleSendSms() {
        let tel = this.data.volunteerMobile
        let isSend = this.data.isSend
        let reg = /^(\+?0?86\-?)?1[3-9]\d{9}$/
        if (!reg.test(tel)) {
            wx.showToast({
                title: '手机号异常',
                icon: 'none'
            })
            return
        }

        if (isSend) {
            return
        }
        this.setData({
            isSend: true
        })
        const that = this
        wx.showLoading({
            title: "发送中"
        })
        sendSms(tel, res => {
            wx.showToast({
                title: '发送成功',
                icon: 'none',
                mask: true,
            })
            const timer = setInterval(function () {
                if (that.data.timerCount <= 0) {
                    clearInterval(timer)
                    that.setData({
                        isSend: false,
                        timerCount: 60
                    })
                } else {
                    that.setData({
                        timerCount: that.data.timerCount - 1
                    })
                }
            }, 1000)
        })
    },

    // 账号更新
    accountChange(e) {
        this.setData({
            volunteerAccount: e.detail
        })
    },

    // 密码更新
    passChange(e) {
        this.setData({
            volunteerPass: e.detail
        })
    },

    // 姓名更新
    nameChange(e) {
        this.setData({
            volunteerName: e.detail
        })
    },


    // 手机号更新
    mobileChange(e) {
        this.setData({
            volunteerMobile: e.detail
        })
    },

    // 短信验证码更新
    smsChange(e) {
        this.setData({
            smsCode: e.detail
        })
    },
    handleGetInfo() {

    },
    // 授权信息
    handleGetPower(e) {
        if (e.detail.userInfo) {
            wx.showToast({
                title: '授权成功',
            })
            this.setData({
                powerShow: false
            })
            let userInfo = e.detail.userInfo
            this.setData({
                volunteerSex: userInfo.gender ? '男' : '女',
                volunteerAvatar: userInfo.avatarUrl,
                volunteerName: userInfo.nickName
            })

            // return this.handleGetInfo()
        } else {
            wx.showToast({
                title: '授权失败',
                icon: 'none'
            })
        }
    }
})